//
//  FinderViewModel.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/3.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "FinderViewModel.h"

@implementation FinderViewModel
-(void)fetchFinderCatWithCmp:(void(^)(NSMutableArray *catList))cmp{
    [[HttpClient shareClient] get:SYSDIC params:@{@"dictType":@"YXW01"} suc:^(id results) {
        NSMutableArray *categoryList = [[NSMutableArray alloc]init];
        NSArray *catList = [NSArray arrayWithArray:results];
        for (int i=0;i<[catList count]; i++) {
            NSDictionary *dic = [catList objectAtIndex:i];
            [categoryList addObject:[SysDicModel modelObjectWithDictionary:dic]];
        }
        cmp(categoryList);
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)fetchBannerList:(void(^)(NSMutableArray *bannerList))cmp{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setDictionary:@{@"order": @{},
                            @"p": @(0),
                            @"search": @{},
                            @"size": @(10),
                            @"blurSearch":@{}
                            }];
    [[HttpClient shareClient]postWithUrl:BANNERLIST params:params suc:^(id results) {
        NSMutableArray *bannerList = [[NSMutableArray alloc]init];
        NSArray *banners = [NSArray arrayWithArray:[results objectForKey:@"resultList"]];
        for (int i=0;i<[banners count]; i++) {
            NSDictionary *dic = [banners objectAtIndex:i];
            [bannerList addObject:[BannerListModel modelObjectWithDictionary:dic]];
        }
        cmp(bannerList);
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)fetchFinderList:(NSString*)typeCode keyWord:(NSString*)keyword cmp:(void(^)(NSMutableArray *finderList))cmp{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setDictionary:@{@"order": @{},
                            @"p": @(0),
                            @"search": @{@"typeCode":typeCode},
                            @"size": @(10)
                            }];
    if (!EC_ISNullStr(keyword)) {
        [params setObject:@{@"title":keyword} forKey:@"blurSearch"];
    }else{
        [params setObject:@{} forKey:@"blurSearch"];
    }
    [[HttpClient shareClient]postWithUrl:ATICLELIST params:params suc:^(id results) {
                                                                 NSMutableArray *finderList = [[NSMutableArray alloc]init];
                                                                 NSArray *finderlist = [NSArray arrayWithArray:[results objectForKey:@"resultList"]];
                                                                 for (int i=0;i<[finderlist count]; i++) {
                                                                     NSDictionary *dic = [finderlist objectAtIndex:i];
                                                                     [finderList addObject:[FinderListModel modelObjectWithDictionary:dic]];
                                                                 }
                                                                 cmp(finderList);
                                                             } fail:^(NSString *error, NSInteger status) {
                                                                 Toast(error);
                                                             }];
}
-(void)fetchFinderDetail:(NSString*)finderId cmp:(void(^)(FinderDetailModel *detailModel))cmp{
    [[HttpClient shareClient]get:ARTICLEDETAIL params:@{@"id":finderId} suc:^(id results) {
        cmp([FinderDetailModel modelObjectWithDictionary:results]);
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}

-(void)addToLikeList:(NSString*)articleId cmp:(void(^)())cmp{
    [[HttpClient shareClient] postWithUrl:ARTICLE_ADDFAV params:@{@"articleId":articleId,@"userId":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)delFromLikeList:(NSString*)articleId cmp:(void(^)())cmp{
    [[HttpClient shareClient] postWithUrl:ARTICLE_DELFAV params:@{@"articleId":articleId,@"userId":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)addToTopList:(NSString*)articleId cmp:(void(^)())cmp{
    [[HttpClient shareClient] postWithUrl:ARTICLE_ADDLIKE params:@{@"articleId":articleId,@"userId":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)delFromTopList:(NSString*)articleId cmp:(void(^)())cmp{
    [[HttpClient shareClient] postWithUrl:ARTICLE_DELLIKE params:@{@"articleId":articleId,@"userId":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
@end
